/**
 * @author Created by qiush on 2017-06-28.
 */

import {Action} from 'redux';

import immutableObject from '../common/immutableObject';
import {ActionType} from './ActionType';

export interface ScrollTopState {
  [designId: string]: number;
}

export interface ScrollTopAction extends Action {
  type: ActionType;
  designId?: string;
  scrollTop?: number;
}

function scrollTopReducer(state: ScrollTopState = {}, action: ScrollTopAction): ScrollTopState {
  switch (action.type) {
    case ActionType.UPDATE_SCROLL_TOP:
      return _updateScrollTop(state, action.designId, action.scrollTop);

    default:
      return state;
  }
}

function _updateScrollTop(state: ScrollTopState, designId: string, scrollTop: number): ScrollTopState {
  if (!designId) {
    return state;
  }
  return immutableObject.set(state, designId, scrollTop);
}

export default scrollTopReducer;
